package com;


import com.Qieces.*;
import com.client.Client;
import com.client.JoinRoom;
import com.entity.Request;
import com.server.CreateRoom;
import com.server.Server;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;


public class MyPanel extends JPanel {



    //用于判断回合所属者
    int index = 0;

    //棋子创建
    Qiece[] qieces = new Qiece[0];

    int i,j;

    //背景构建
    CheckerBoard board = new CheckerBoard();

    Client client;
    Server server;

    public MyPanel() {
        try {
            client = new JoinRoom("2","3").joinRoom();
            server = new CreateRoom("2").createRoom();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    //贴图绘制
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        board.paintSelf(g);
        for (Qiece q:qieces){
            q.paintSelf(g);
        }
    }


    //鼠标点击事件（返回点击坐标）
    int x,y,t;          //t用于区分第一次和第二次点击
    public void mouseAction(){
        MouseAdapter mouse = new MouseAdapter(){
            @Override
            public void mousePressed(MouseEvent e){

                //黑方执棋（还没写）
                if(e.getButton()==MouseEvent.BUTTON1 && index == 0){

                    //读取鼠标点击的位置
                    x = e.getX();
                    y = e.getY();
                    int A=(y-20)/90;
                    int B=(x-62)/90;
                    //对一个棋子的两步进行点击操作
                    if(selectOf(x,y) && t==0){
                        t=1;
                        i=A;
                        j=B;
                        System.out.println("!!!");
                        for(int i=0;i<10;i++){
                            for (int j=0;j<9;j++) {
                                System.out.print(i*9+j + "," + qieces[i*9+j].getKind() + "," + qieces[i*9+j].getMoveof() + "<" + qieces[i*9+j].getX() + "," + qieces[i*9+j].getY() + "> \t");
                            }
                            System.out.println();
                        }
                        System.out.println("???");

                        repaint();
                        return;
                    }
                    if(t==1){
                        if (client!=null){
                            Request request = new Request(x,y,i,j);
                            client.send(request);
                        }

                        moveSelected(x,y,i,j);
                        t=0;
                        //重画
                        repaint();
                    }
                }

                //红方执棋（还没写）
//                else if(e.getButton()==MouseEvent.BUTTON1 && index == 1){
//                    x = e.getX();
//                    y = e.getY();
//                    System.out.println(100);
//                    index = 0;
//                }

                //可视化输出90个棋子的底层数据
                for(int i=0;i<10;i++){
                    for (int j=0;j<9;j++) {
                        System.out.print(i*9+j + "," + qieces[i*9+j].getKind() + "," + qieces[i*9+j].getMoveof() + "<" + qieces[i*9+j].getX() + "," + qieces[i*9+j].getY() + "> \t");
                    }
                    System.out.println();
                }

                //输出点击点坐标
                System.out.println(x+","+y);
                System.out.println();
            }
        };
        this.addMouseListener(mouse);
        this.setFocusable(true);
    }


    //初始化棋盘上90个棋子
    public void CreatQieces(){
        //将90个可移动地块置为死棋类
        for (int i = 0; i < 10; i++){
            for (int j = 0; j < 9; j++){
                Qiece o = new Death(i+1,j+1);
                qieces = Arrays.copyOf(qieces,qieces.length + 1);
                qieces[qieces.length - 1] = o;
            }
        }

        //将
        qieces[4] = new General(qieces[4], 0, 0);
        qieces[85] = new General(qieces[85], 0, 1);

        //卒
        for (int i = 0; i < 5; i++) {
            qieces[27 + i * 2] = new Soldier(qieces[27 + i * 2], 0, 0);
            qieces[54 + i * 2] = new Soldier(qieces[54 + i * 2], 0, 1);
        }

        for (int i = 0; i < 2; i++) {
            //车
            qieces[0+i*8] = new Chariot(qieces[0+i*8],0,0);
            qieces[81+i*8] = new Chariot(qieces[81+i*8],0,1);
            //马
            qieces[1+i*6] = new Horse(qieces[1+i*6], 0, 0);
            qieces[82+i*6] = new Horse(qieces[82+i*6], 0, 1);
            //炮
            qieces[19+i*6] = new Cannon(qieces[19+i*6], 0, 0);
            qieces[64+i*6] = new Cannon(qieces[64+i*6], 0, 1);
            //相
            qieces[2+i*4] = new Minister(qieces[2+i*4], 0, 0);
            qieces[83+i*4] = new Minister(qieces[83+i*4], 0, 1);
            //士
            qieces[3+i*2] = new Assistant(qieces[3+i*2], 0, 0);
            qieces[84+i*2] = new Assistant(qieces[84+i*2], 0, 1);
        }

    }

    //通过遍历所有棋子，判断是否有棋子的位置与鼠标点击实践坐标重合(目前只写了车)
    public boolean selectOf(int x,int y){
        int i=(y-20)/90;            //范围大于图片范围，需修改！！！！！！！
        int j=(x-62)/90;
        if (qieces[i*9+j].select(x,y)){
            qieces[i*9+j].movingRange(qieces,i,j);
            return true;
        }
        return false;
    }

    //通过遍历所有棋子，对已选择的棋子进行移动
    public void moveSelected(int x,int y,int i,int j){

        int A=(y-20)/90;            //范围大于图片范围，需修改！！！！！！！
        int B=(x-62)/90;

        if (qieces[i*9+j].getScl() == 1 && qieces[A*9+B].getMoveof() == 1){

            final int a = qieces[A*9+B].getX();
            final int b = qieces[A*9+B].getY();
            System.out.println(a+"，"+b);
            Qiece o = new Death(i+1,j+1);

            switch (qieces[i*9+j].getKind()){
                case 1 : {
                    o = new General(qieces[i*9+j],1,1);
                    break;
                }
                case 2 : {
                    o = new Assistant(qieces[i*9+j],1,1);
                    break;
                }
                case 3 : {
                    o = new Minister(qieces[i*9+j],1,1);
                    break;
                }
                case 4 : {
                    o = new Chariot(qieces[i*9+j],1,1);
                    break;
                }
                case 5 : {
                    o = new Horse(qieces[i*9+j],1,1);
                    break;
                }
                case 6 : {
                    o = new Cannon(qieces[i*9+j],1,1);
                    break;
                }
                case 7 : {
                    o = new Soldier(qieces[i*9+j],1,1);
                    break;
                }
            }

            o.setX(a);
            o.setY(b);
            qieces[A*9+B] = o;
            qieces[i*9+j].setKind(0);
            qieces[i*9+j].setScl(0);
        }

        for (int I = 0; I < 10; I++) {
            for (int J = 0; J < 9; J++) {
                qieces[I*9+J].setMoveof(0);
            }
        }

    }
}

